#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify, request,session
from service.userInfoService import UserInfoService
from utils import commons
from utils.loggings import loggings
from utils.response_code import RET
from utils.verify_code import get_verifycode
class UserInfoOtherResource(Resource):

    @classmethod
    def myquery(cls):
        userid = request.args.get('UserID')
        res = UserInfoService.myquery(userid)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'],
                           )
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])
    # join table query
    @classmethod
    def joint_query(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserID', type=str, location='args', required=False, help='UserID参数类型不正确或缺失')
        parser.add_argument('Account', type=str, location='args', required=False, help='UserOpenID参数类型不正确或缺失')
        parser.add_argument('FaceImg', type=str, location='args', required=False, help='FaceImg参数类型不正确或缺失')
        parser.add_argument('UserNickName', type=str, location='args', required=False, help='UserNickName参数类型不正确或缺失')
        parser.add_argument('IsDelete', type=int, location='args', required=False, help='IsDelete参数类型不正确或缺失')
        parser.add_argument('CreateTime', type=str, location='args', required=False, help='CreateTime参数类型不正确或缺失')
        parser.add_argument('ModifyTime', type=str, location='args', required=False, help='ModifyTime参数类型不正确或缺失')

        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = UserInfoService.joint_query(**kwargs)
        if res['code'] == RET.OK:
            loggings.info(1,'/api_1_0/userInfo/query')
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalCount=res['totalCount'], totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    # 验证码
    @classmethod
    def getverifycode(cls):
            data = get_verifycode()
            session.clear()
            session['verifycode'] = data
            return {'code':2000,'message':'OK','verifycoode':data}

    @classmethod
    def loginv1(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('Account', type=str, location='form', required=True, help='Account参数类型不正确或缺失')
        parser.add_argument('Password', type=str, location='form', required=True, help='password参数类型不正确或缺失')
        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = UserInfoService.user_loginv1(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data']
                           )
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def login(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('Account', type=str, location='form', required=True, help='Account参数类型不正确或缺失')
        parser.add_argument('Password', type=str, location='form', required=True, help='password参数类型不正确或缺失')
        parser.add_argument('VerifyCode', type=str, location='form', required=True, help='VerifyCode参数类型不正确或缺失')
        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = UserInfoService.user_login(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data']
                           )
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def commentme(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserID', type=str, location='args', required=True, help='UserID参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = UserInfoService.commentme(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalCount=res['totalCount'],
                           totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def replyme(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserID', type=str, location='args', required=False, help='UserID参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = UserInfoService.replyme(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalCount=res['totalCount']
                          )
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def upvoteme(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserID', type=str, location='args', required=True, help='UserID参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = UserInfoService.upvoteme(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalCount=res['totalCount'],
                           totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def search_byname(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserNickName', type=str, location='args', required=True, help='UserNickName参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = UserInfoService.search_byname(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalCount=res['totalCount'],
                           totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])


    @classmethod
    def recommend(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserID', type=str, location='args', required=True, help='UserID参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = UserInfoService.recommend(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])